import java.util.concurrent.LinkedBlockingQueue;
/*
 ByGpt
class MyTask implements Runnable {
    private final String name;

    public MyTask(String name) {
        this.name = name;
    }

    @Override public void run() {
        System.out.println("Task " + name + " executed at: " + System.currentTimeMillis());
    }
}

class CustomTimer {
    private final BlockingQueue<ScheduledTask> taskQueue;
    private final TimerThread timerThread;

    public CustomTimer() {
        taskQueue = new LinkedBlockingQueue<>();
        timerThread = new TimerThread();
        // timerThread.start(); // 不直接启动线程 }

        public void schedule (Runnable learn.task,long delay){
            long executionTime = System.currentTimeMillis() + delay;
            taskQueue.offer(new ScheduledTask(learn.task, executionTime));
            // 启动 TimerThread，如果它还没有运行 if (!timerThread.isAlive()) {
            timerThread.start(); // 启动定时器线程 }
        }

        private class TimerThread extends Thread {
            @Override
            public void run() {
                while (true) {
                    try {
                        ScheduledTask scheduledTask = taskQueue.take(); // 阻塞直到有任务 long waitTime = scheduledTask.executionTime - System.currentTimeMillis();
                        if (waitTime > 0) {
                            Thread.sleep(waitTime); // 等待直到任务可以执行 }
                            new Thread(scheduledTask.learn.task).start(); // 在新线程中执行任务 } catch (InterruptedException e) {
                            Thread.currentThread().interrupt(); // 恢复中断状态 break; //退出循环 }
                        }
                    }
                }

                private static class ScheduledTask {
                    private final Runnable learn.task;
                    private final long executionTime;

                    public ScheduledTask(Runnable learn.task, long executionTime) {
                        this.learn.task = learn.task;
                        this.executionTime = executionTime;
                    }
                }
            }

            public class TimerExample {
                public static void main(String[] args) {
                    CustomTimer learn.timer = new CustomTimer();
                    learn.timer.schedule(new MyTask("Task1"), 2000); //2秒后执行 learn.timer.schedule(new MyTask("Task2"),1000); //1秒后执行 }
                }
            }
        }
    }
}
 */
